package com.pinyougou.sellergoods.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.pojo.TbBrandExample.Criteria;
import com.pinyougou.sellergoods.service.BrandService;

import entity.PageResult;

@Service
public class BrandServiceImpl<brandMapper> implements BrandService {

	@Autowired
	private TbBrandMapper brandMapper;

	@Override
	public List<TbBrand> findAll() {

		return brandMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {

		PageHelper.startPage(pageNum, pageSize);

		Page<TbBrand> PageList = (Page<TbBrand>) brandMapper.selectByExample(null);

		return new PageResult(PageList.getTotal(), PageList.getResult());
	}

	@Override
	// 增加品牌
	public void add(TbBrand tbBrand) {
		brandMapper.insert(tbBrand);

	}

	@Override
	// 根据id查找品牌
	public TbBrand findOne(long id) {
		// 根据主键id 查找品牌
		TbBrand brand = brandMapper.selectByPrimaryKey(id);
		return brand;
	}

	@Override
	// 修改品牌
	public void update(TbBrand tbBrand) {
		// 根据主键,修改品牌
		brandMapper.updateByPrimaryKey(tbBrand);

	}

	@Override
	// 批量删除
	public void deleBrand(long[] ids) {

		for (long id : ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	// 根据条件查询
	public PageResult findPage(TbBrand tbBrand, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		TbBrandExample example = new TbBrandExample();
		Criteria createCriteria = example.createCriteria();

		if (tbBrand != null) {
			if (tbBrand.getName() != null && tbBrand.getName().length() > 0) {
				createCriteria.andNameLike("%" + tbBrand.getName() + "%");
			}
			if (tbBrand.getFirstChar() != null && tbBrand.getFirstChar().length() > 0) {
				createCriteria.andFirstCharEqualTo(tbBrand.getFirstChar());
			}
		}
		Page<TbBrand> Page = (Page<TbBrand>) brandMapper.selectByExample(example);
		
		return new PageResult(Page.getTotal(), Page.getResult());
	}

}
